﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.StockMasterSVC;
using System.Collections.ObjectModel;

namespace SMS.Model
{
    public class StockMasterModel
    {
        private ServiceProxy<StockMasterServiceClient> proxy = null;
        public event Action<ObservableCollection<SMS.Entities.StockMaster>> OnLoadStockMasterCompleted;
        public event Action<SMS.Entities.StockMaster> OnGetCompleteStockmasterCompleted;
        public event Action<int> OnSaveStockMaster;
        public event Action<SMS.Entities.SMSException> OnServiceError;
        public event Action<ObservableCollection<SMS.Entities.ProductStockWise>> OnGetProductsByStockCompleted;
        public event Action<int> OnProductStockWiseSaved;
        public event Action<SMS.Entities.ProductStockWise> OnGetCompletedProductStockWiseCompleted;
        public StockMasterModel()
        {
            proxy = new ServiceProxy<StockMasterServiceClient>("Service/StockMasterService.svc");
            proxy.Client.LoadStockMasterListCompleted += new EventHandler<LoadStockMasterListCompletedEventArgs>(Client_LoadStockMasterListCompleted);
            proxy.Client.GetCompleteStockMasterCompleted += new EventHandler<GetCompleteStockMasterCompletedEventArgs>(Client_GetCompleteStockMasterCompleted);
            proxy.Client.SaveStockMasterCompleted += new EventHandler<SaveStockMasterCompletedEventArgs>(Client_SaveStockMasterCompleted);
            proxy.Client.GetProductsByStockCompleted += new EventHandler<GetProductsByStockCompletedEventArgs>(Client_GetProductsByStockCompleted);
            proxy.Client.GetCompleteProductStockDetailCompleted += new EventHandler<GetCompleteProductStockDetailCompletedEventArgs>(Client_GetCompleteProductStockDetailCompleted);
            proxy.Client.SaveProductStockWiseCompleted += new EventHandler<SaveProductStockWiseCompletedEventArgs>(Client_SaveProductStockWiseCompleted);
        }

        void Client_SaveProductStockWiseCompleted(object sender, SaveProductStockWiseCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnProductStockWiseSaved != null)
                    OnProductStockWiseSaved(e.Result);
            }
            else
            {
                OnServiceError(e.serviceError);
            }
        }

        void Client_GetCompleteProductStockDetailCompleted(object sender, GetCompleteProductStockDetailCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnGetCompletedProductStockWiseCompleted != null)
                    OnGetCompletedProductStockWiseCompleted(e.Result);
            }
            else
            {
                OnServiceError(e.serviceError);
            }
        }

        void Client_GetProductsByStockCompleted(object sender, GetProductsByStockCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnGetProductsByStockCompleted != null)
                    OnGetProductsByStockCompleted(e.Result);
            }
            else
            {
                OnServiceError(e.serviceError);
            }
        }

        void Client_SaveStockMasterCompleted(object sender, SaveStockMasterCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnSaveStockMaster(e.Result);
            }
            else
            {
                if (OnServiceError != null)
                    OnServiceError(e.serviceError);
            }
        }

        void Client_GetCompleteStockMasterCompleted(object sender, GetCompleteStockMasterCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnGetCompleteStockmasterCompleted(e.Result);
            }
            else
            {
                if (OnServiceError != null)
                    OnServiceError(e.serviceError);
            }
        }

        void Client_LoadStockMasterListCompleted(object sender, LoadStockMasterListCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnLoadStockMasterCompleted(e.Result);
            }
            else
            {
                if (OnServiceError != null)
                    OnServiceError(e.serviceError);
            }
        }

        public void LoadStockMasterList(string userName, DateTime fromDate, DateTime endDate)
        {
            proxy.Client.LoadStockMasterListAsync(userName, fromDate, endDate);
        }
        public void GetCompleteStockMaster(string userName, int StockID)
        {
            proxy.Client.GetCompleteStockMasterAsync(userName, StockID);
        }
        public void SaveStockMaster(string userName, SMS.Entities.StockMaster Obj)
        {
            proxy.Client.SaveStockMasterAsync(userName, Obj);
        }

        public void GetProductsByStock(string userName, int StockID)
        {
            proxy.Client.GetProductsByStockAsync(userName, StockID);
        }

        public void GetCompleteProduct(string userName, int ProductStockID)
        {
            proxy.Client.GetCompleteProductStockDetailAsync(userName, ProductStockID);
        }

        public void SaveProductStockWise(string userName, SMS.Entities.ProductStockWise obj)
        {
            proxy.Client.SaveProductStockWiseAsync(userName, obj);
        }

    }
}
